package com.st.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.st.entity.Emp;
import com.st.service.IEmpService;
import com.st.util.UploadUtil;

@RequestMapping("/emp/")
@Controller
public class EmpController {

	@Autowired
	private IEmpService empService;

	/**
	 * 页面跳转
	 */
	@GetMapping("/list.do")
	public String empList() {
		return "emp/list";
	}

	@GetMapping("/add.do")
	public String empAdd() {
		return "emp/add";
	}

	@GetMapping("/modify.do")
	public ModelAndView empModify(Integer eid, ModelAndView mv) {
		mv.addObject("eid", eid);
		mv.setViewName("emp/modify");
		return mv;
	}

	/**
	 * 获取所有员工的信息
	 */
	@RequestMapping("/getAll.json")
	@ResponseBody
	public List getAll() {
		// 参数
		// 业务
		List emps = empService.getAll();
		for (Object object : emps) {
			System.err.println(object);
		}
		// 结果
		return emps;
	}

	/**
	 * 根据ID获取员工信息
	 */
	@RequestMapping("/get.json")
	@ResponseBody
	public Emp get(Integer eid) {
		System.out.println("返回员工信息");
		// 参数
		// 业务
		Emp emp = empService.getById(eid);
		System.out.println(emp);
		System.err.println("-----------------------------------------------------------");
		// 结果
		return emp;
	}

	/**
	 * 根据ID删除员工
	 * 
	 * @throws IOException
	 */
	@RequestMapping("/remove.do")
	// @ResponseBody
	public void remove(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 获取参数
		String strId = request.getParameter("eid");
		System.out.println(strId);
		int eid = Integer.parseInt(strId);
		int res = empService.delectById(eid);
		response.setContentType("text/html;charset=utf-8");
		if (res > 0) {
			// 删除成功
			response.getWriter().write("<script>alert('删除成功'); window.location.href='list.jsp';</script>");
		} else {
			// 删除失败
			response.getWriter().write("<script>alert('删除失败'); window.location.href='list.jsp';</script>");
		}
	}

	/**
	 * 添加员工信息
	 */
	@PostMapping("/add.do")
	public ModelAndView add(Emp emp, MultipartFile file, HttpServletRequest request, ModelAndView mv) {
		// 1.获取参数
		String ephoto = UploadUtil.saveFile(file, request, "upload");
		if (ephoto != null) {
			emp.setEphoto(ephoto);
		}
		// 2.处理业务
		int res = empService.add(emp);
		// 3.返回结果
		if (res > 0) {
			// 成功
			mv.setViewName("redirect:list.do");
		} else {
			// 失败
			mv.addObject("error", "添加失败");
			mv.setViewName("emp/add");
		}
		return mv;
	}
	
	/**
	 * 修改员工信息
	 */
	@PostMapping("/modify.do")
	public ModelAndView modify(Emp emp,MultipartFile file,HttpServletRequest request,ModelAndView mv) {
		//1.获取参数
		String ephoto = UploadUtil.saveFile(file, request, "upload");
		if(ephoto != null) {
			emp.setEphoto(ephoto);;
		}
		//2.处理业务
		int res = empService.modify(emp);
		//3.返回结果
		if(res > 0) {
			//成功
			mv.setViewName("redirect:list.do");
		}else {
			//失败
			mv.addObject("error", "修改失败");
			mv.setViewName("emp/modify");
		}
		return mv;
	}
}
